package com.qk.management.controller;

import com.qk.common.PageResult;
import com.qk.common.Result;
import com.qk.dto.role.RoleDTO;
import com.qk.entity.Role;
import com.qk.management.aop.anno.LogAnno;
import com.qk.management.service.RoleService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author 上官徵羽
 * @version 1.0
 * @since 2025-10-08 7:19
 */
@Slf4j
@RequestMapping("/roles")
@RestController
@SuppressWarnings("all")
public class RoleController {
    @Autowired
    private RoleService roleService;
    @GetMapping
    public Result listPage(RoleDTO roleDTO){
        PageResult<Role> pageResult = roleService.listPage(roleDTO);
        return Result.success(pageResult);
    }
    @LogAnno
    @DeleteMapping("/{id}")
    public Result delete(@PathVariable Integer id){
        log.info("删除角色id:{}", id);
        roleService.deleteById(id);
        return Result.success();
    }
    @LogAnno
    @PostMapping
    public Result addRole(@RequestBody Role role){
        log.info("添加角色:{}", role);
        roleService.addRole(role);
        return Result.success();
    }
    @GetMapping("/{id}")
    public Result getRoleById(@PathVariable Integer id){
        Role role = roleService.getById(id);
        return Result.success(role);
    }
    @LogAnno
    @PutMapping
    public Result updateRole(@RequestBody Role role){
        log.info("更新角色:{}", role.getId());
        roleService.updateRole(role);
        return Result.success();
    }

    @GetMapping("/list")
    public Result listAll(){
        List<Role> list = roleService.listAll();
        return Result.success(list);
    }
}
